package ro.savatech.pg.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LayoutManager;
import java.awt.geom.Ellipse2D;
import java.util.Calendar;
import java.util.List;

import javax.swing.JFrame;

import ro.savatech.pg.flightlog.Flight;
import de.erichseifert.gral.data.DataTable;
import de.erichseifert.gral.plots.XYPlot;
import de.erichseifert.gral.plots.lines.DefaultLineRenderer2D;
import de.erichseifert.gral.plots.lines.LineRenderer;
import de.erichseifert.gral.plots.points.PointRenderer;
import de.erichseifert.gral.ui.InteractivePanel;

@SuppressWarnings("serial")
public class FlightPlot extends JFrame {
	private Flight flight;

	@SuppressWarnings("unchecked")
	public FlightPlot(Flight aFlight) {
		this.flight = aFlight;

		setSize(800, 300);

		int delta = this.flight.getEntry().getDelta();
		List<Short> seria = this.flight.getAlt1Seria();
		Calendar time = this.flight.getEntry().getStartTime();

		DataTable data = new DataTable(Long.class, Short.class);
		for (Short alt1Val : seria) {
			data.add(time.getTimeInMillis(), alt1Val);
			time.add(Calendar.SECOND, delta);
		}

		XYPlot plot = new XYPlot(data);
		LineRenderer lines = new DefaultLineRenderer2D();
		plot.setLineRenderer(data, lines);
		plot.getPointRenderer(data).setSetting(PointRenderer.SHAPE,
				new Ellipse2D.Double(-1, -1, 2, 2));

		LayoutManager layout = new GridBagLayout();
		getContentPane().setLayout(layout);

		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.gridx = 0;
		c.gridy = 0;

		getContentPane().add(new InteractivePanel(plot), c);

		c.weighty = 0;
		c.gridx = 0;
		c.gridy = 1;
		getContentPane().add(new FlightInfoPanel(this.flight), c);
	}
}
